<?php 

namespace Admin\Controller;
use Think\Controller;
use Think\Log;
use Org\Util\Page;
 

class MenuController extends BaseController {
	function _initialize() {
		parent::_initialize ();
	}

	public function index() { 
		$m = M("wx_menu"); 
		$keyword=I('keyword',"","trim");
		$where="";
		
		if($keyword)
		{
			$where= "(mname like '%".$keyword."%' or keyword like '%".$keyword."%' or `menutype` like '%".$keyword."%')";
		} 
		$count= $m->where($where)->count();

		// 查询满足要求的总记录数
		$Page = new Page ( $count, 12 ); // 实例化分页类 传入总记录数和每页显示的记录数
		$Page -> setConfig('header', '条记录');
		$Page -> setConfig('theme', '<li><a>%totalRow% %header%</a></li> <li>%upPage%</li> <li>%downPage%</li> <li>%first%</li>  <li>%prePage%</li>  <li>%linkPage%</li>  <li>%nextPage%</li> <li>%end%</li> ');//(对thinkphp自带分页的格式进行自定义)
		$show = $Page->show (); // 分页显示输出 

		$result =  $m->where($where)->order("listorder desc")->limit ( $Page->firstRow . ',' . $Page->listRows )->select ();
		  
		 
		$this->assign ( "page", $show ); // 赋值分页输出
		$this->assign ( "menu", $result );
		 
		$this->display ();
	}
	
	
	public function add()
	{
		$menuid=I("id");
	
		$menuid= isset($menuid)?$menuid:0;
		$menuid =empty($menuid)?0:$menuid; 
		 
		if($menuid>0)
			$ret= M("wx_menu")->where("menuid=$menuid")->find();
	
		$wxmenu=M("wx_menu")->where("pid=0 and status=1")->order("listorder desc")->select();
		$route=M("wx_route")->select();
		 
		$this->assign ( "entity", $ret );
		$this->assign ( "parentmenu", $wxmenu );
		$this->assign ( "wxmenu", $route ); 
		$this->display ();
	}
	


	public function addmenu() { 
		$data = $_POST;
		
		/*  
		// 防止各个公网环境的不一致性。在提交微信菜单的时候获取当前域名地址，需要保证本地提交的域名问题。
		$url=$data['url'];
		if(!empty($url))
		{
			$flg= (strpos($url, "Services/")!==false && strpos($url, "http://")===false);
			$data['url']= ($flg?C('WEBURL').C("WXConfigPathUrl").'/':'').$url;
		}*/
		
		$data["operationuser"] =UID;
		$data["createtime"] = date("Y-m-d H:i:s");

		$dao=D("wx_menu");
		if(!$dao->create($data)){$this->error($dao->getError());}

		if (empty($_POST["menuid"])||$_POST["menuid"] == 0) {
			unset ( $data ["menuid"] );
			$result = $dao->add ( $data ); 
		}
		else
		{
			$result = $dao->save ( $data ); 
		}
		
		$exe_scriptstr = "<script type='text/javascript'>
                setTimeout(function(){
					 var win=   art.dialog.opener;
                     win.location.reload();
                     art.dialog.close();
                },300);
           </script>";
		
	   if ($result) {
			$this->success ( "操作成功!".$exe_scriptstr);
		}
		else{
			$this->error ( "删除失败！".$exe_scriptstr);
		}
	} 
	 

	public function delmenu() { 
		$menuid=I("id");
		if(empty($menuid)){
			$this->error("传递参数错误!");
		}
		$where="FIND_IN_SET(menuid,'$menuid')";
		$result = M("wx_menu")->where($where)->delete(); 
		if($result){
			$this->success ( "删除成功" );
		}else
		{
			$this->error ( "删除失败！" );
		}
	}
	
	
	
	//微信菜单更新
	public function createMenu() {
		$menu = M("wx_menu")->where("status=1")->order("listorder desc")->select();
		$newary=array();
		foreach($menu as $k=>$v){
			if($v["menutype"] == "view"){
				$flg= (strpos($v["url"], "Services/")!==false);
				$v['url']=($flg?C('WEBURL').C("WXConfigPathUrl").'/':'').$v["url"];
			}else{
				$v['key']=$v["key"];
			}
			$newary[]=$v;
		}
	
		$wxmenu=$this->createwxmune($newary);
		//var_dump($wxmenu);
	
		$wxObj = $this->init();
		$ret=$wxObj->createMenu($wxmenu);
		 
		if($ret){
			$this->success("重新创建菜单成功!");
		}
		else {
			$this->success("重新创建菜单失败!");
		}
	}
	
}

?>